<?php
/**
 * @author shutyuka@gmail.com
 * @modified 2011-02-20
 */
class OauthLimit{
	
	static public $OAUTH_PREENGAGE = 0;
	
	static public $OAUTH_ADMIN = 10;
	
	static public $TABLE_NAME="oauth_limit";
	
	

	static public function Create($userId, $oauthId = 10) {
		$oauth = self::GetByUserIdAndOauth($userId, $oauthId);
		if( $oauth != null ){
			return $oauth["oauth_id"];
		}
		$oauthLimit = array();
		$oauthLimit['user_id'] = $userId;
		$oauthLimit['oauth_id'] = $oauthId;
		$oauthLimit['create_time'] = time();
		DB::Insert(self::$TABLE_NAME, $oauthLimit);
		
		return $oauthLimit['oauth_id'];
	}
	
	static public function GetByUserId($userId){
		$oc = array( 
			'user_id' => $userId
			);
		$all = DB::LimitQuery(self::$TABLE_NAME, array(
				'condition' => $oc
				));
		return $all;
	}
		
	static public function GetAllUserId(){
		$sql = "select distinct(`user_id`) as user_id from ".self::$TABLE_NAME;
		$all = DB::GetQueryResult($sql, false);
		return $all;
	}
	
	static public function getOauthMap($userId){
		$oauthArray = self::GetByUserId($userId);
		$oauthMap = array();
		foreach ($oauthArray as $key => $value){
			//Logs::setLog("sdfsdfsdfsdf=========".$value["oauth_id"]);
			$oauthMap[$value["oauth_id"]] = $value;
		}
		return $oauthMap;
	}
	
	static public function GetByUserIdAndOauth($userId, $oauth){
		$oc = array( 
			'user_id' => $userId, 
			'oauth_id' => $oauth
			);
		$all = DB::LimitQuery(self::$TABLE_NAME, array(
				'condition' => $oc
				));
		return $all;
	}
	
	static public function DelAuth($userId){
		$sql = "delete from ".self::$TABLE_NAME." where  user_id=$userId";
		DB::Query($sql);
		return ;
	}
}
